package com.heima.redis实现单例;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author 陈辉
 * @data 2023 11:22
 */
@RestController
public class SingleController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * synchronized在分布式环境下，无法保证数据安全，因为多服务器间，不能形成资源互斥。
     *
     * @return
     */
    @GetMapping("/single")
    public String stock01() {
        redisTemplate.opsForValue().setIfAbsent("single" , new Student().toString());
        Object stu = redisTemplate.opsForValue().get("single");
        System.out.println("stu = " + stu);
        return "ok";
    }


}
